{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'> <class 'set'> <class 'set'> <class 'set'> <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 初始化\n",
    "a = set()\n",
    "b = {2,3,4}\n",
    "c = {2,}\n",
    "d = set([2,3,3])\n",
    "e = set((23,4,5,4))\n",
    "\n",
    "print(type(a), type(b), type(c), type(d), type(e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加一个d:  {2, 3, 4, 'e'}\n",
      "删除2：  {3, 4, 'e'}\n",
      "删除e：  {3, 4}\n",
      "随机pop：  {4}\n",
      "并集：  {4, 12, 13, 14}\n"
     ]
    }
   ],
   "source": [
    "# 1.增 add, 2.删remove,pop,discard  3.改update求并集, 4.查\n",
    "d = set([2,3,3,4]) \n",
    "d.add('e')         \n",
    "print('添加一个d: ', d)\n",
    "\n",
    "d.remove(2)              # 没有元素， 就报错\n",
    "print('删除2： ', d)\n",
    "\n",
    "d.discard('e')           # 不会报错\n",
    "print('删除e： ', d)\n",
    "\n",
    "d.pop()                  # 随机删除一个元素\n",
    "print('随机pop： ', d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = {2, 3, 4, 5, 6, 7, 'e'}\n",
      "c = {2, 3, 4, 5, 6, 7, 'e'}\n",
      "a 异于b：  {2, 3}\n",
      "a交b:  {4, 5}\n",
      "False\n",
      "a: {4, 5}  b: {4, 5, 6, 7, 'e'} True\n",
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{2, 3, 6, 7, 'e'}"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集，并集，余集，异\n",
    "a = {2,3,4,5}\n",
    "b = {4,5,6,7,'e'}\n",
    "a.update(b)              # update 并集，更新原集合\n",
    "print(f'{a = }')\n",
    "c = a.union(b)           # union 并集，新建一个集合\n",
    "print(f'{c = }')\n",
    "\n",
    "print('a 异于b： ', a.difference(b))  # diffrence 求异\n",
    "a.difference(a, b)\n",
    "a.difference_update(b, b)            \n",
    "\n",
    "a = {2,3,4,5}\n",
    "b = {4,5,6,7,'e'}\n",
    "print('a交b: ', a.intersection(b))   # 交集，新建集合\n",
    "a.intersection_update(b)             # 交集，更新原集合\n",
    "\n",
    "print(a.isdisjoint(b) )                # isdisjoint 是否完全不相交\n",
    "print('a:',a, ' b:', b, a.issubset(b)) # a是否是b的子集\n",
    "print(a.issuperset(b))\n",
    "\n",
    "a = {2,3,4,5}\n",
    "b = {4,5,6,7,'e'}\n",
    "a.symmetric_difference(b)           # 余集，新建一个集合\n",
    "a.symmetric_difference_update(b)    # 余集, 更新原集合\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
